import request from "@/libs/request";

const basePath = '/appcloud-dashboard' ;

export const apiUrls = {
    getContainerDashboard : basePath + '/api/container/getContainerDashboard',
    getContainerInfo : basePath + '/api/container/getContainerInfo',
    getContainerNet : basePath + '/api/container/getContainerNet',
}


//根据容器id查询容器监控信息
export async function getContainerDashboard(params){
	return request( apiUrls.getContainerDashboard , {
      method:'POST',
      body:params
  }).then(ret=>{
        let data = {}
        if(ret&&ret.data){
            // CPU
            if(ret.data.cpuUsedPercent){
                var cpuUsedPercent = parseFloat(ret.data.cpuUsedPercent).toFixed(2);
                var cpuUnUsePercent = (100-cpuUsedPercent).toFixed(2);
            }else{
                cpuUsedPercent = '0';cpuUnUsePercent = '0'
            }
            
            if(ret.data.memoryTotal && ret.data.memoryUsed && ret.data.memoryUsedPercent){
                 // 内存
                var ncUsedPersent = parseFloat(ret.data.memoryUsedPercent).toFixed(2);
                var ncUnUsePersent = (100 - ncUsedPersent).toFixed(2);
            }else{
                ncUsedPersent = 0
                ncUnUsePersent = 0
                ret.data.memoryUsed = '0'
                ret.data.memoryTotal ='0'
            }
           
            data = {
                cpuRow : [
                    {name : `CPU已使用${cpuUsedPercent}%`, value : cpuUsedPercent || "0"},
                    {name : `CPU未使用${cpuUnUsePercent}%`, value : cpuUnUsePercent || "0"},
                ],
                ncRow : [
                    {name : `内存使用${ret.data.memoryUsed}`, value : ncUsedPersent || "0"},
                    {name : `内存总量${ret.data.memoryTotal}`, value : ncUnUsePersent || "0"},
                ],
            }
        }
        
        return data ;
  })
}

//根据ip查询容器名称
export async function getContainerInfo(params){
	return request( apiUrls.getContainerInfo , {
      method:'POST',
      body:params
  })
}

//根据容器id查询容器监控信息
export async function getContainerNet(params){
	return request( apiUrls.getContainerNet , {
      method:'POST',
      body:params
  })
}
